package tq.com.tqteacher.activity.newPublish;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;

import com.githang.statusbar.StatusBarCompat;

import java.util.ArrayList;

import tq.com.systools.system.AlertDialogHelper;
import tq.com.systools.system.StringHelper;
import tq.com.tqcom.base.BaseActivity;
import tq.com.tqcom.net.INetCallback;
import tq.com.tqcom.net.TQNetResult;
import tq.com.tqcom.util.TQEventBus;
import tq.com.tqcom.util.TQSystemDefine;
import tq.com.tqcom.view.TQHeader5View;
import tq.com.tqteacher.R;
import tq.com.tqteacher.model.TQLetShareModel;
import tq.com.tqteacher.model.TQLogin;
import tq.com.tqteacher.model.TQPublishType;
import tq.com.tqteacher.view.TQBasePublishInfoView;
import tq.com.tqteacher.view.TQHeader3ViewHold;

public class TQLetShareActivity extends BaseActivity {
    private TQHeader5View mHeader5View;
    private TQBasePublishInfoView mBasePublishInfoView;

    private EditText mEditText;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_letshare_layout);
        setStatusBar();

        mHeader5View = findViewById(R.id.title);
        mHeader5View.getTitleView().setText("请ta分享");
        mHeader5View.getBackIcon().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TQLetShareActivity.this.finish();
            }
        });

        TQBasePublishInfoView.GradData lGradData = new TQBasePublishInfoView.GradData(TQLogin.getCurrent().getUserInfo().getGrade(),
                TQSystemDefine.gradeName(TQLogin.getCurrent().getUserInfo().getGrade()));
        ArrayList<TQBasePublishInfoView.GradData> lGradData1 = new ArrayList<>();
        lGradData1.add(lGradData);

        mBasePublishInfoView = findViewById(R.id.baseInfo);
        mBasePublishInfoView.setGradData(lGradData1);

        findViewById(R.id.next).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goNext();
            }
        });

        mEditText = findViewById(R.id.maxShare);

        if(TQPublishType.getSharePublishItem() != null && TQPublishType.getSharePublishItem().isModify()){
            mBasePublishInfoView.setPublishContent(TQPublishType.getSharePublishItem());

            if(TQPublishType.getSharePublishItem() instanceof TQLetShareModel){
                TQLetShareModel lModel = (TQLetShareModel)TQPublishType.getSharePublishItem();

                mEditText.setText(String.valueOf(lModel.getShareCount()));
            }
        }
    }

    private void goNext(){
        if(!mBasePublishInfoView.checkValid()){
            return;
        }
        String content = StringHelper.editText(mEditText);
        if(!StringHelper.isNumeric(content) || content.length() == 0){
            AlertDialogHelper.show(this, "分享人数只能为数字");
            return;
        }
        int maxShare = Integer.valueOf(content);
        if(maxShare == 0){
            AlertDialogHelper.show(this, "最大分享人数不能为0");
            return;
        }

        TQPublishType.setSharePublishItemBaseInfo(mBasePublishInfoView.title(), mBasePublishInfoView.desc(),
                mBasePublishInfoView.getCurrentGrade().getId());
        TQLetShareModel lModel = (TQLetShareModel)TQPublishType.getSharePublishItem();
        lModel.setShareCount(maxShare);

        if(lModel.isModify()){
            TQPublishType.getSharePublishItem().onModifyBaseInfo(new INetCallback() {
                @Override
                public void result(TQNetResult pNetResult) {
                    if(!pNetResult.isSuccess()){
                        AlertDialogHelper.show(TQLetShareActivity.this, pNetResult.getErrMsg());
                        return;
                    }

                    TQEventBus.post(new TQEventBus.TQEventMessage(TQEventBus.TQEventMessage.Opertor.modify, null));
                    TQLetShareActivity.this.finish();
                }
            });
        }else{
            lModel.onPulishComplete(new INetCallback() {
                @Override
                public void result(TQNetResult pNetResult) {
                    if(!pNetResult.isSuccess()){
                        AlertDialogHelper.show(TQLetShareActivity.this, pNetResult.getErrMsg());
                        return;
                    }

                    TQPublishType.getSharePublishItem().onNewPublishResult(pNetResult.getData());

                    TQEventBus.post(new TQEventBus.TQEventMessage(TQEventBus.TQEventMessage.Opertor.add, null));
                    TQLetShareActivity.this.finish();
                    TQPublishType.showPublishNextStep(TQLetShareActivity.this);
                }
            });
        }


    }
}
